package RabbitMq.Controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.UUID;

/**
 * @ClassName RabbitProvider
 * @Description TDD
 * @Author 混沌酱
 * @Date 2023-10-12 10:01
 * @Version 1.0
 **/
@RestController
@RequestMapping("bootRabbitMq")
public class RabbitProvider {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendNewYearMessage")
    public String sendMessage() {
        String messageId = String.valueOf(UUID.randomUUID());   // 随机一个消息 ID
        String messageContent = "快过年了，提前祝你新年快乐。";   // 消息主题内容
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap<String, Object> messageMap = new HashMap<>();
        messageMap.put("messageId", messageId);
        messageMap.put("messageContent", messageContent);
        messageMap.put("sendTime", sendTime);

        /*
         * 发送消息
         * 参数1：交换机名称
         * 参数2：路由 routeKey
         * 参数3：消息主题内容
         */
        rabbitTemplate.convertAndSend("boot_topic_exchange", "happyNewYear.o.i", messageMap);
        return "<p style=color:blue;text-align:center;top:20px;font-size:28px;>新年祝福已发送</p>";
    }

    @GetMapping("/sendNewYearMessage01")
    public String sendNewYearMessage01() {
        for (int m = 0; m <= 100; m++) {
            String messageId = String.valueOf(UUID.randomUUID());   // 随机一个消息 ID
            String messageContent = "快过年了，提前祝你新年快乐。";   // 消息主题内容
            String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            HashMap<String, Object> messageMap = new HashMap<>();
            messageMap.put("messageId", messageId);
            messageMap.put("messageContent", messageContent);
            messageMap.put("sendTime", sendTime);

            /*
             * 发送消息
             * 参数1：交换机名称
             * 参数2：路由 routeKey
             * 参数3：消息主题内容
             */
            rabbitTemplate.convertAndSend("boot_topic_exchange", "happyNewYear.o.i", messageMap);
        }
        return "<p style=color:blue;text-align:center;top:20px;font-size:28px;>新年祝福已发送</p>";
    }

}
